# load packages
library(geosphere)
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(ggthemes)
library(RColorBrewer)
library(sp)
library(maptools)
## Checking rgeos availability: FALSE
## Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
## which has a restricted licence. It is disabled by default;
## to enable gpclib, type gpclibPermit()
library(maps)
library(data.table)
library(tidyverse)
## -- Attaching packages ---------------------------------- tidyverse 1.3.0 --
## v tibble 2.1.3 v dplyr 0.8.3
## v tidyr 1.0.0 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## v purrr 0.3.3
## -- Conflicts ------------------------------------- tidyverse_conflicts() --
## x dplyr::between() masks data.table::between()
## x dplyr::filter() masks plotly::filter(), stats::filter()
## x dplyr::first() masks data.table::first()
## x dplyr::lag() masks stats::lag()
## x dplyr::last() masks data.table::last()
## x purrr::map() masks maps::map()
## x purrr::transpose() masks data.table::transpose()
library(DT)
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
##
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
##
## wind
library(mapproj)
library(rgdal)
## rgdal: version: 1.4-7, (SVN revision 845)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
## Path to GDAL shared files: C:/Users/ydeng.AIR/Documents/R/win-library/3.5/rgdal/gdal
## GDAL binary built with GEOS: TRUE
## Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
## Path to PROJ.4 shared files: C:/Users/ydeng.AIR/Documents/R/win-library/3.5/rgdal/proj
## Linking to sp version: 1.3-2
library(leaflet)
library(ggrepel)
# set working directory
setwd("C:/Users/ydeng.AIR/Desktop/Evelyn/1. Columbia/04. DSPP/Presentation 1 - Crime")
# load data
base::load("police_incidents_df.rdata")
#base::load("uk_aggregated.rdata")
#base::load("uk_aggregated_anti_social.rdata")
# view data
head(police_incidents,10)
#head(uk_aggregated,1000)
#head(uk_aggregated_anti_social, 1000)
# choose only necessary variables
crime <- police_incidents[c(2,4,5,6,10)]
crime
table(crime$Crime.type)
##
## Anti-social behaviour Bicycle theft
## 5507770 272692
## Burglary Criminal damage and arson
## 1254892 1702860
## Drugs Other crime
## 430853 220388
## Other theft Possession of weapons
## 1531002 91439
## Public order Robbery
## 755325 174401
## Shoplifting Theft from the person
## 1077225 257796
## Vehicle crime Violence and sexual offences
## 1193693 3615919
# create dataset with aggregated crime by month by type
crime_freq_by_month <- crime %>%
group_by(Month, Crime.type) %>%
summarise(frequency = n())
crime_freq_by_month
graph <- ggplot(crime_freq_by_month, aes(x = Month, y = frequency)) +
geom_line(aes(group=Crime.type, color = Crime.type)) +
#geom_point(size = 2, shape = 21, fill = "lightblue") +
theme(legend.position = "top") +
labs(x="Month", y="Number of Crimes") +
ggtitle("Monthly Crime Frequency in the UK") +
theme_hc() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(graph, tooltip = c("x", "y", "group"), dynamicTicks=TRUE)
ASB is the most frequent crime, followed by violent and sexual offenses
seasonal crime peaks: - anti-social behavior(ASB) summer - violent and sexual offences summer - other theft summer - bicycle theft summer - public order summer - burglary fall to early winter - theft from the person early winter (December)
the seasonality of crime is most notable for: - ASB (approx 45% more crime during peak season) - bike theft (approx 100% more crime during peak season)
Note: Increase in crime is not a reflection of increase in population, which was only about 2% from 2014 to 2017 (office for national statistics: https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates/articles/overviewoftheukpopulation/august2019)